#!/bin/bash

#Submit this script with: sbatch thefilename

#SBATCH --time=720:00:00   # walltime
#SBATCH --ntasks-per-node=64
#SBATCH --nodes=1   # number of nodes
#SBATCH --mem=500G   # memory per node
#SBATCH -J "cp2k"   # job name



# set input file name here
export FullNameInputFile=H2O-128.inp


# LOAD MODULES, INSERT CODE, AND RUN YOUR PROGRAMS HERE
ulimit -d unlimited
ulimit -s unlimited
ulimit -t unlimited
ulimit -v unlimited

export SLURM_EXPORT_ENV=ALL


cd $SLURM_SUBMIT_DIR
username=`whoami`

### here, suppose /tmp is the I/O storage. 

CP2K_RUNDIR=/tmp/${username}.${SLURM_JOB_ID}

if [ ! -a $CP2K_RUNDIR ]; then
   echo "Scratch directory $CP2K_RUNDIR created."
   mkdir -p $CP2K_RUNDIR
fi

cp -r $SLURM_SUBMIT_DIR/* $CP2K_RUNDIR

cd $CP2K_RUNDIR

spack load cp2k@9.1

echo "Starting CP2K run at `hostname` on:" `date` >> ${SLURM_JOB_ID}.log

srun --mpi=pmi2 cp2k.psmp ${FullNameInputFile} 1>cp2k.out 2>cp2k.err

echo "Finished CP2K run at `hostname `on:" `date` >> ${SLURM_JOB_ID}.log
cat $SLURM_JOB_NODELIST >> ${SLURM_JOB_ID}.log

mv $CP2K_RUNDIR/* $SLURM_SUBMIT_DIR
rm -fr $CP2K_RUNDIR

